package com.lkx.farmershop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.farmershop.common.api.ApiResult;
import com.lkx.farmershop.mapper.FollowMapper;
import com.lkx.farmershop.model.dto.FollowDto;
import com.lkx.farmershop.model.entity.Follow;
import com.lkx.farmershop.model.vo.FollowAndFansCount;
import com.lkx.farmershop.model.vo.SimpleUserVo;
import com.lkx.farmershop.service.FollowService;
import com.lkx.farmershop.service.IFarmerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Follow)表服务实现类
 *
 * @author makejava
 * @since 2025-01-29 16:37:10
 */
@Service("followService")
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Autowired
    private IFarmerService farmerService;

    @Override
    public ApiResult<String> followUser(FollowDto followDto) {
        Long userId = followDto.getUserId();
        Long followedUserId = followDto.getFollowedUserId();
        Boolean isFollowed = followDto.getIsFollowed();
        if(isFollowed){
            save(new Follow(userId,followedUserId));
            return ApiResult.success("关注成功");
        }else{
            LambdaQueryWrapper<Follow> qw = new LambdaQueryWrapper<>();
            qw.eq(Follow::getUserid,userId).eq(Follow::getFollowedUserid,followedUserId);
            remove(qw);
            return ApiResult.success("取消关注成功");
        }
    }

    @Override
    public ApiResult<FollowAndFansCount> getCount(Long userId) {

        Integer followCount = lambdaQuery().eq(Follow::getUserid, userId).count();
        Integer fansCount = lambdaQuery().eq(Follow::getFollowedUserid, userId).count();

        return ApiResult.success(new FollowAndFansCount(followCount,fansCount));

    }

    @Override
    public ApiResult<List<SimpleUserVo>> getFollowList(Long userId) {
        //查询此用户所有关注
        List<Follow> followList = lambdaQuery().eq(Follow::getUserid, userId).list();
        if(followList==null || followList.isEmpty()){
            return ApiResult.success(null);
        }
        //查询所有被关注者的id
        List<Long> idList = followList.stream()
                .map(Follow::getFollowedUserid).collect(Collectors.toList());
        //获取被关注者的信息
        List<SimpleUserVo> simpleUserVoList = farmerService.listByIds(idList).stream()
                .map(farmer -> new SimpleUserVo(farmer.getFarmerId(),farmer.getName(),farmer.getAvatar()))
                .collect(Collectors.toList());

        return ApiResult.success(simpleUserVoList);
    }

    @Override
    public ApiResult<List<SimpleUserVo>> getFansList(Long userId) {
        //查询此用户所有粉丝
        List<Follow> fansList = lambdaQuery().eq(Follow::getFollowedUserid, userId).list();
        if(fansList==null || fansList.isEmpty()){
            return ApiResult.success(null);
        }
        //查询所有粉丝的id
        List<Long> idList = fansList.stream()
                .map(Follow::getUserid).collect(Collectors.toList());
        //获取粉丝的信息
        List<SimpleUserVo> simpleUserVoList = farmerService.listByIds(idList).stream()
                .map(farmer -> new SimpleUserVo(farmer.getFarmerId(),farmer.getName(),farmer.getAvatar()))
                .collect(Collectors.toList());

        return ApiResult.success(simpleUserVoList);
    }
}

